<template>
  <div class="app-container">

    <el-form :inline="true" :model="filter" class="demo-form-inline">
      <el-form-item label="状态">
        <el-select v-model="filter.status" clearable>
          <el-option v-for="(item, index) in dictionary.Status" :key="index" :label="item" :value="parseInt(index)"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="类型">
        <el-select v-model="filter.type" clearable>
          <el-option v-for="(item, index) in dictionary.TagType" :key="index" :label="item" :value="parseInt(index)"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="handleAdd()">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading"
              :data="page.list" stripe border
              style="width: 100%">
      <el-table-column
        prop="code"
        label="编码" width="100">
      </el-table-column>
      <el-table-column
        prop="name"
        label="名称" min-width="100">
      </el-table-column>
      <el-table-column
        prop="type"
        label="类型" width="100">
        <template slot-scope="scope">
          <span>{{ dictionary.TagType[scope.row.type] }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        label="状态" width="100">
        <template slot-scope="scope">
          <span>{{ dictionary.Status[scope.row.status] }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="updatedBy"
        label="更新人" width="150">
      </el-table-column>
      <el-table-column
        prop="updatedDt"
        label="更新时间" width="150">
        <template slot-scope="scope">
          <span>{{ new Date(scope.row.updatedDt) | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button-group>
            <el-button v-if="scope.row.status!==1"
                       size="mini"
                       type="primary"
                       @click="handleRelease(scope.$index, scope.row)">发布
            </el-button>

            <el-button v-if="scope.row.status===1"
                       size="mini"
                       type="warning"
                       @click="handleRecall(scope.$index, scope.row)">撤回
            </el-button>
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑
            </el-button>
            <el-button size="mini"
                       type="danger"
                       @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </el-button-group>
          <el-button-group>
            <el-button
              size="mini" type="success"
              @click="handleContent(scope.$index, scope.row)">管理相关内容
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination style="margin-top:20px;"
                   @size-change="handleSizeChange"
                   @current-change="handleCurrentChange"
                   :current-page="page.number"
                   :page-sizes="[10, 20, 50, 100]"
                   :page-size="page.size"
                   layout="total, sizes, prev, pager, next, jumper"
                   :total="page.totalElements">
    </el-pagination>

    <el-dialog title="编辑" :visible.sync="form.visible">
      <tag-edit ref="form" @saved="handleSaved"></tag-edit>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex';
// import TagApi from '@/api/mathsgo/tag'
import TagEdit from './TagEdit';

export default {
    components: { TagEdit },
    data() {
        return {
            loading: 0,
            filter: {
                type: null
            },
            page: {
                totalPage: 1,
                current: 1,
                totalElements: 2,
                size: 10,
                list: []
            },
            form: {
                visible: false
            }
        };
    },
    computed: {
        ...mapGetters([
            'dictionary'
        ])
    },
    created() {
        this.getPage();
    },
    methods: {
        getPage() {
            this.loading++;
        // TagApi.fetchPage(this.filter, this.page)
        //   .then(data => {
        //     this.loading--
        //     this.page = Object.assign(this.page, data.obj)
        //   })
        //   .catch(error => {
        //     this.loading--
        //     console.error(error)
        //   })
        },
        commandById: function(id, entity, callback) {
            this.loading++;
            entity.id = id;
        // TagApi.save(entity)
        //   .then(data => {
        //     this.loading--
        //     callback && callback()
        //   })
        },
        // 发布
        handleRelease: function(index, row) {
            this.commandById(row.id, { status: 1 }, () => {
                this.$set(this.page.list[index], 'status', 1);
            });
        },
        // 取消发布
        handleRecall: function(index, row) {
            this.commandById(row.id, { status: 0 }, () => {
                this.$set(this.page.list[index], 'status', 0);
            });
        },
        // 查询
        handleQuery: function() {
            this.getPage();
        },
        // 编辑
        handleEdit: function(index, row) {
            this.openEdit(row.id);
        },
        // 添加
        handleAdd: function() {
            this.openEdit('');
        },
        // 删除
        handleDelete: function(index, row) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
            }).then(() => {
                this.loading++;
                // TagApi.remove(row.id)
                //   .then(() => {
                //     this.loading--
                //     this.$message({
                //       type: 'success',
                //       message: '删除成功!'
                //     })
                //     this.getPage()
                //   })
            });
        },
        handleSizeChange: function(val) {
            this.page.size = val;
            this.getPage();
        },
        handleCurrentChange: function(val) {
            this.page.current = val;
            this.getPage();
        },
        handleContent(index, row) {
        // dictionary.TagType:  { 0: '知识点', 1: '题库' },
            if (row.type === 1) {
                this.$router.push({ path: '/problem/storage-manager-page', query: { tagId: row.id }});
            }
        },
        openEdit: function(id) {
            this.form.visible = true;
            setTimeout(() => {
                this.$refs['form'].init(id);
            }, 10);
        },
        handleSaved() {
            this.getPage();
        }
    }
};
</script>

<style>

</style>
